package cn.com.dcsgo.controller;

import cn.com.dcsgo.common.PageInfo;
import cn.com.dcsgo.common.Result;
import cn.com.dcsgo.domain.dto.SysUserQueryDTO;
import cn.com.dcsgo.domain.dto.SysUserSaveDTO;
import cn.com.dcsgo.domain.dto.SysUserUpdateDTO;
import cn.com.dcsgo.domain.po.SysUser;
import cn.com.dcsgo.permission.MyPermission;
import cn.com.dcsgo.service.SysUserService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.RequiredArgsConstructor;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.io.Serializable;

/**
 * SysUserController
 *
 * @author Dcsgo
 * @since 2/6/2025 下午 1:30
 */
@Tag(name = "用户")
@RestController
@RequestMapping("sysUser")
@RequiredArgsConstructor
public class SysUserController extends BaseController {
    private final SysUserService sysUserService;

    @MyPermission({"system:user:query"})
    @Operation(summary = "分页查询用户信息")
    @PostMapping("getByPage")
    public Result<PageInfo<SysUser, SysUserQueryDTO>> getPage(@RequestBody PageInfo<SysUser, SysUserQueryDTO> pageInfo) {
        PageInfo<SysUser, SysUserQueryDTO> page = sysUserService.page(pageInfo);
        return new Result<>(page);
    }

    @MyPermission({"system:user:info"})
    @Operation(summary = "根据id获取用户信息")
    @GetMapping("get/{userId}")
    public Result<SysUser> get(@PathVariable Long userId) {
        return new Result<>(sysUserService.getOneWithDeptNameAndLeaderName(userId));
    }

    @MyPermission({"system:user:add"})
    @Operation(summary = "添加用户")
    @PostMapping("save")
    public Result<Serializable> save(@Validated @RequestBody SysUserSaveDTO saveDTO) {
        int row = sysUserService.add(saveDTO);
        return chooseReturn(row > 0);
    }

    @MyPermission({"system:user:update"})
    @Operation(summary = "修改用户信息")
    @PutMapping("update")
    public Result<Serializable> save(@Validated @RequestBody SysUserUpdateDTO updateDTO) {
        int row = sysUserService.update(updateDTO);
        return chooseReturn(row > 0);
    }

    @MyPermission({"system:user:remove"})
    @Operation(summary = "删除用户")
    @DeleteMapping("delete/{userId}")
    public Result<Serializable> delete(@PathVariable Long userId) {
        int row = sysUserService.remove(userId);
        return chooseReturn(row > 0);
    }

    @MyPermission({"system:user:resetPwd"})
    @Operation(summary = "重置用户密码")
    @GetMapping("resetPwd/{userId}")
    public Result<Serializable> resetPassword(@PathVariable Long userId) {
        int row = sysUserService.resetPassword(userId);
        return chooseReturn(row > 0);
    }
}
